package com.imdroid.lite.net.udp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;

import android.location.Location;

import com.imdroid.lite.IMDroid;
import com.imdroid.lite.model.Team;

@Deprecated
public class UDPSendUtil {
	
	public static void sendAudioFile(String fileName){
		FileInputStream fis = null;
		try{
			File file = new File(IMDroid.TEMP_FOLDER + fileName);
			int len = (int)file.length();
			short totalParts = (short)(len / 512);
			if(len % 512 != 0){
				totalParts ++;
			}
			byte[] controls = new byte[20];
			controls[0] |= 1 << 1; // large file transfer
			controls[0] |= 1 << 2; // broadcast
			controls[1] = FileTypes.IDA_AUDIO;
			System.arraycopy(IntegerTransfer.longToByte(System.currentTimeMillis()), 0, controls, 2, 8); // msgId
			System.arraycopy(IntegerTransfer.intToByte(IMDroid.getUser().getUser_id()), 0, controls, 10, 4); // user id
			System.arraycopy(IntegerTransfer.intToByte((int)Team.getCurrentTeam().getTeamId()), 0, controls, 14, 4); // team id
			System.arraycopy(IntegerTransfer.shortToByte(totalParts), 0, controls, 18, 2);
			fis = new FileInputStream(file);
			for(short i=0; i<totalParts + 1; i++){
				byte[] dataToSend = new byte[512 + 22];
				System.arraycopy(controls, 0, dataToSend, 0, controls.length);
				int sLen = 0;
				if(i == 0){
					byte[] phead = (fileName + "&&" + IMDroid.getUser().getUser_name() + "&&" + len).getBytes("UTF-8");
					System.arraycopy(phead,
							0, dataToSend, 22, phead.length);
					sLen = 22 + phead.length;
				} else {
					System.arraycopy(IntegerTransfer.shortToByte(i), 0, dataToSend, 20, 2);
					sLen = 22 + fis.read(dataToSend, 22, 512);
				}
				DatagramPacket packet = new DatagramPacket(dataToSend, sLen);
				packet.setAddress(ServerAddress.address);
				packet.setPort(ServerAddress.port);
				UDPServer.sendPacket(packet);
				if(i == 0){
					Thread.sleep(20); // wait the file name to send first
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			try {fis.close();} catch (IOException e) {}
		}
	}
	
	public static void sendTeamLocation(Location loc){
		StringBuilder locStr = new StringBuilder();
		locStr.append(Team.getCurrentTeam().getTeamId()).append("&&");
		locStr.append(loc.getLatitude()).append("&&");
		locStr.append(loc.getLongitude()).append("&&");
		locStr.append(loc.getSpeed()).append("&&");
		locStr.append(IMDroid.getUser().getUser_name()).append("&&");
		locStr.append(loc.getBearing()).append("&&1");// 1 is device type
		byte[] payload = null;
		try {
			payload = locStr.toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		byte[] dataToSend = new byte[18 + payload.length];
		dataToSend[0] |= 1 << 2; // broadcast
		dataToSend[1] = FileTypes.TEAM_LOCATION;
		System.arraycopy(IntegerTransfer.longToByte(System.currentTimeMillis()), 0, dataToSend, 2, 8); // msgId
		System.arraycopy(IntegerTransfer.intToByte(IMDroid.getUser().getUser_id()), 0, dataToSend, 10, 4); // user Id
		System.arraycopy(IntegerTransfer.intToByte((int)Team.getCurrentTeam().getTeamId()), 0, dataToSend, 14, 4);
		System.arraycopy(payload, 0, dataToSend, 18, payload.length);
		DatagramPacket packet = new DatagramPacket(dataToSend, dataToSend.length);
		packet.setAddress(ServerAddress.address);
		packet.setPort(ServerAddress.port);
		UDPServer.sendPacket(packet);
	}

}
